﻿Imports System.Globalization
Imports System.IO
Partial Public Class Report_Tug
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "Tug Report"
        End If
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Function CreateDataTB() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("DWT", GetType(String))
        dt.Columns.Add("Full", GetType(String))
        dt.Columns.Add("Light", GetType(String))
        dt.Columns.Add("Cost", GetType(String))
        dt.Columns.Add("Tug_Domestic", GetType(String))
        dt.Columns.Add("Use_Domestic", GetType(String))
        dt.Columns.Add("Price_Domestic", GetType(String))
        dt.Columns.Add("Tug_Foreign", GetType(String))
        dt.Columns.Add("Use_Foreign", GetType(String))
        dt.Columns.Add("Price_Foreign", GetType(String))
        dt.Columns.Add("Type_Event", GetType(String))
        dt.Columns.Add("Type_Text", GetType(String))
        dt.AcceptChanges()

        For i As Integer = 0 To 8
            Dim dr As DataRow = dt.NewRow
            Select Case (i)
                Case 0
                    dr.Item("DWT") = "น้อยกว่า   2,500"
                    dr.Item("Full") = "1"
                    dr.Item("Light") = "1"
                    dr.Item("Cost") = "7,000 บาท/เที่ยว/WORKIING BOAT"
                Case 1
                    dr.Item("DWT") = ""
                    dr.Item("Full") = ""
                    dr.Item("Light") = ""
                    dr.Item("Cost") = "15,000 บาท/เที่ยว/ TUG"
                Case 2
                    dr.Item("DWT") = "2,501 - 5,000"
                    dr.Item("Full") = "1"
                    dr.Item("Light") = "1"
                    dr.Item("Cost") = "30,000 บาท/เที่ยว/TUG"
                Case 3
                    dr.Item("DWT") = "5,001 – 10,000"
                    dr.Item("Full") = "1"
                    dr.Item("Light") = "1"
                    dr.Item("Cost") = "55,000 บาท/4ชม/TUG"
                Case 4
                    dr.Item("DWT") = " 10,001 – 49,999"
                    dr.Item("Full") = "2"
                    dr.Item("Light") = "2"
                    dr.Item("Cost") = "55,000 บาท/4ชม/TUG"
                Case 5
                    dr.Item("DWT") = "50,000 – 119,999"
                    dr.Item("Full") = "3"
                    dr.Item("Light") = "2"
                    dr.Item("Cost") = "55,000 บาท/4ชม/TUG"
                Case 6
                    dr.Item("DWT") = "120,000 – 199,999"
                    dr.Item("Full") = "4"
                    dr.Item("Light") = "3"
                    dr.Item("Cost") = "55,000 บาท/4ชม/TUG"
                Case 7
                    dr.Item("DWT") = "200,000 – 249,999"
                    dr.Item("Full") = "5"
                    dr.Item("Light") = "3"
                    dr.Item("Cost") = "55,000 บาท/4ชม/TUG"
                Case 8
                    dr.Item("DWT") = "250,000 – 350,000"
                    dr.Item("Full") = "6"
                    dr.Item("Light") = "4"
                    dr.Item("Cost") = "55,000 บาท/4ชม/TUG"
            End Select


            dr.Item("Tug_Domestic") = "0"
            dr.Item("Use_Domestic") = "0"
            dr.Item("Price_Domestic") = "0"
            dr.Item("Tug_Foreign") = "0"
            dr.Item("Use_Foreign") = "0"
            dr.Item("Price_Foreign") = "0"
            dr.Item("Type_Event") = "P"
            dr.Item("Type_Text") = "Cash in"

            dt.Rows.Add(dr)
            dt.AcceptChanges()
        Next
        For i As Integer = 0 To 8
            Dim dr As DataRow = dt.NewRow
            Select Case (i)
                Case 0
                    dr.Item("DWT") = "น้อยกว่า   2,500"
                    dr.Item("Full") = "1"
                    dr.Item("Light") = "1"
                    dr.Item("Cost") = "7,000 บาท/เที่ยว/WORKIING BOAT"
                Case 1
                    dr.Item("DWT") = ""
                    dr.Item("Full") = ""
                    dr.Item("Light") = ""
                    dr.Item("Cost") = "15,000 บาท/เที่ยว/ TUG"
                Case 2
                    dr.Item("DWT") = "2,501 - 5,000"
                    dr.Item("Full") = "1"
                    dr.Item("Light") = "1"
                    dr.Item("Cost") = "30,000 บาท/เที่ยว/TUG"
                Case 3
                    dr.Item("DWT") = "5,001 – 10,000"
                    dr.Item("Full") = "1"
                    dr.Item("Light") = "1"
                    dr.Item("Cost") = "55,000 บาท/4ชม/TUG"
                Case 4
                    dr.Item("DWT") = " 10,001 – 49,999"
                    dr.Item("Full") = "2"
                    dr.Item("Light") = "2"
                    dr.Item("Cost") = "55,000 บาท/4ชม/TUG"
                Case 5
                    dr.Item("DWT") = "50,000 – 119,999"
                    dr.Item("Full") = "3"
                    dr.Item("Light") = "2"
                    dr.Item("Cost") = "55,000 บาท/4ชม/TUG"
                Case 6
                    dr.Item("DWT") = "120,000 – 199,999"
                    dr.Item("Full") = "4"
                    dr.Item("Light") = "3"
                    dr.Item("Cost") = "55,000 บาท/4ชม/TUG"
                Case 7
                    dr.Item("DWT") = "200,000 – 249,999"
                    dr.Item("Full") = "5"
                    dr.Item("Light") = "3"
                    dr.Item("Cost") = "55,000 บาท/4ชม/TUG"
                Case 8
                    dr.Item("DWT") = "250,000 – 350,000"
                    dr.Item("Full") = "6"
                    dr.Item("Light") = "4"
                    dr.Item("Cost") = "55,000 บาท/4ชม/TUG"
            End Select


            dr.Item("Tug_Domestic") = "0"
            dr.Item("Use_Domestic") = "0"
            dr.Item("Price_Domestic") = "0"
            dr.Item("Tug_Foreign") = "0"
            dr.Item("Use_Foreign") = "0"
            dr.Item("Price_Foreign") = "0"
            dr.Item("Type_Event") = "F"
            dr.Item("Type_Text") = "Internal Charge"

            dt.Rows.Add(dr)
            dt.AcceptChanges()
        Next
        Return dt
    End Function

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        If (txtDate.Text.Trim = "") Or (txtDateTo.Text.Trim = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุช่วงระยะเวลาการค้นหา');", True)
            Exit Sub
        End If
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet
        dsReport.Tables.Add(Get_Tug())
        dsReport.AcceptChanges()
        If (dsReport.Tables(0).Rows.Count > 0) Then
            ReportName = "Report_TUG.rdlc"
            ' dsReport = New DataSet
            reportDataSource = "Tug_DataTable1"
            paraName = "DateTimeNow"
            paraValue = DateTime.Now.ToString()
            Orientation = False
            reportType = radioType.SelectedValue

            urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

            Dim oldFile As String = String.Empty
            oldFile = Server.MapPath(urlDisplay)
            Dim fileInfo As FileInfo = New FileInfo(oldFile)
            Dim reportPath As String = "../WriteReport/"

            Dim newFileName As String = DateTime.Now.ToString("_yyMMdd_HHmmss", New CultureInfo("en-US"))
            newFileName = "Tug_Report" & newFileName

            If fileInfo.Exists Then
                fileInfo.MoveTo(fileInfo.DirectoryName & "/" & newFileName & fileInfo.Extension)
                urlDisplay = reportPath & newFileName & fileInfo.Extension
            End If

            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ไม่พบข้อมูลการใช้งานเรือทัก');", True)
            Exit Sub
        End If

    End Sub

    Private Function Get_Tug() As DataTable
        Dim D_From As String = txtDate.Text.Split(" ")(2) & CDate(txtDate.Text).ToString("MMdd")
        Dim D_To As String = txtDateTo.Text.Split(" ")(2) & CDate(txtDateTo.Text).ToString("MMdd")
        Dim dtValue As DataTable = clsGet.Get_Tug_Report(D_From, D_To, radioPOrt.SelectedValue)
        Dim dtTug As DataTable = CreateDataTB()
        For Each dr As DataRow In dtValue.Rows
            If (dr.Item("Type_Event") & "" = "P") Then
                If (CDbl(dr.Item("Vehicle_DWT")) < 2501) Then
                    If (dr.Item("MATERIAL") & "" = "000000002100000236") Then
                        If (dr.Item("Vehicle_Destination") & "" = "I") Then
                            dtTug.Rows(1).Item("Tug_Domestic") = CInt(dtTug.Rows(1).Item("Tug_Domestic")) + 1
                            dtTug.Rows(1).Item("Use_Domestic") = CInt(dtTug.Rows(1).Item("Use_Domestic")) + CInt(dr.Item("data3"))
                            dtTug.Rows(1).Item("Price_Domestic") = CDbl(dtTug.Rows(1).Item("Price_Domestic")) + CDbl(dr.Item("data4"))
                        ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                            dtTug.Rows(1).Item("Tug_Foreign") = CInt(dtTug.Rows(1).Item("Tug_Foreign")) + 1
                            dtTug.Rows(1).Item("Use_Foreign") = CInt(dtTug.Rows(1).Item("Use_Foreign")) + CInt(dr.Item("data3"))
                            dtTug.Rows(1).Item("Price_Foreign") = CDbl(dtTug.Rows(1).Item("Price_Foreign")) + CDbl(dr.Item("data4"))
                        End If
                    ElseIf (dr.Item("MATERIAL") & "" = "000000002100000237") Then
                        If (dr.Item("Vehicle_Destination") & "" = "I") Then
                            dtTug.Rows(0).Item("Tug_Domestic") = CInt(dtTug.Rows(0).Item("Tug_Domestic")) + 1
                            dtTug.Rows(0).Item("Use_Domestic") = CInt(dtTug.Rows(0).Item("Use_Domestic")) + CInt(dr.Item("data3"))
                            dtTug.Rows(0).Item("Price_Domestic") = CDbl(dtTug.Rows(0).Item("Price_Domestic")) + CDbl(dr.Item("data4"))
                        ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                            dtTug.Rows(0).Item("Tug_Foreign") = CInt(dtTug.Rows(0).Item("Tug_Foreign")) + 1
                            dtTug.Rows(0).Item("Use_Foreign") = CInt(dtTug.Rows(0).Item("Use_Foreign")) + CInt(dr.Item("data3"))
                            dtTug.Rows(0).Item("Price_Foreign") = CDbl(dtTug.Rows(0).Item("Price_Foreign")) + CDbl(dr.Item("data4"))
                        End If
                    End If

                ElseIf (CDbl(dr.Item("Vehicle_DWT")) < 5001) Then
                    If (dr.Item("Vehicle_Destination") & "" = "I") Then
                        dtTug.Rows(2).Item("Tug_Domestic") = CInt(dtTug.Rows(2).Item("Tug_Domestic")) + 1
                        dtTug.Rows(2).Item("Use_Domestic") = CInt(dtTug.Rows(2).Item("Use_Domestic")) + CInt(dr.Item("data3"))
                        dtTug.Rows(2).Item("Price_Domestic") = CDbl(dtTug.Rows(2).Item("Price_Domestic")) + CDbl(dr.Item("data4"))
                    ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                        dtTug.Rows(2).Item("Tug_Foreign") = CInt(dtTug.Rows(2).Item("Tug_Foreign")) + 1
                        dtTug.Rows(2).Item("Use_Foreign") = CInt(dtTug.Rows(2).Item("Use_Foreign")) + CInt(dr.Item("data3"))
                        dtTug.Rows(2).Item("Price_Foreign") = CDbl(dtTug.Rows(2).Item("Price_Foreign")) + CDbl(dr.Item("data4"))
                    End If
                ElseIf (CDbl(dr.Item("Vehicle_DWT")) < 10001) Then
                    If (dr.Item("Vehicle_Destination") & "" = "I") Then
                        dtTug.Rows(3).Item("Tug_Domestic") = CInt(dtTug.Rows(3).Item("Tug_Domestic")) + 1
                        dtTug.Rows(3).Item("Use_Domestic") = CInt(dtTug.Rows(3).Item("Use_Domestic")) + CInt(dr.Item("data3"))
                        dtTug.Rows(3).Item("Price_Domestic") = CDbl(dtTug.Rows(3).Item("Price_Domestic")) + CDbl(dr.Item("data4"))
                    ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                        dtTug.Rows(3).Item("Tug_Foreign") = CInt(dtTug.Rows(3).Item("Tug_Foreign")) + 1
                        dtTug.Rows(3).Item("Use_Foreign") = CInt(dtTug.Rows(3).Item("Use_Foreign")) + CInt(dr.Item("data3"))
                        dtTug.Rows(3).Item("Price_Foreign") = CDbl(dtTug.Rows(3).Item("Price_Foreign")) + CDbl(dr.Item("data4"))
                    End If
                ElseIf (CDbl(dr.Item("Vehicle_DWT")) < 50000) Then
                    If (dr.Item("Vehicle_Destination") & "" = "I") Then
                        dtTug.Rows(4).Item("Tug_Domestic") = CInt(dtTug.Rows(4).Item("Tug_Domestic")) + 1
                        dtTug.Rows(4).Item("Use_Domestic") = CInt(dtTug.Rows(4).Item("Use_Domestic")) + CInt(dr.Item("data3"))
                        dtTug.Rows(4).Item("Price_Domestic") = CDbl(dtTug.Rows(4).Item("Price_Domestic")) + CDbl(dr.Item("data4"))
                    ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                        dtTug.Rows(4).Item("Tug_Foreign") = CInt(dtTug.Rows(4).Item("Tug_Foreign")) + 1
                        dtTug.Rows(4).Item("Use_Foreign") = CInt(dtTug.Rows(4).Item("Use_Foreign")) + CInt(dr.Item("data3"))
                        dtTug.Rows(4).Item("Price_Foreign") = CDbl(dtTug.Rows(4).Item("Price_Foreign")) + CDbl(dr.Item("data4"))
                    End If
                ElseIf (CDbl(dr.Item("Vehicle_DWT")) < 120000) Then
                    If (dr.Item("Vehicle_Destination") & "" = "I") Then
                        dtTug.Rows(5).Item("Tug_Domestic") = CInt(dtTug.Rows(5).Item("Tug_Domestic")) + 1
                        dtTug.Rows(5).Item("Use_Domestic") = CInt(dtTug.Rows(5).Item("Use_Domestic")) + CInt(dr.Item("data3"))
                        dtTug.Rows(5).Item("Price_Domestic") = CDbl(dtTug.Rows(5).Item("Price_Domestic")) + CDbl(dr.Item("data4"))
                    ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                        dtTug.Rows(5).Item("Tug_Foreign") = CInt(dtTug.Rows(5).Item("Tug_Foreign")) + 1
                        dtTug.Rows(5).Item("Use_Foreign") = CInt(dtTug.Rows(5).Item("Use_Foreign")) + CInt(dr.Item("data3"))
                        dtTug.Rows(5).Item("Price_Foreign") = CDbl(dtTug.Rows(5).Item("Price_Foreign")) + CDbl(dr.Item("data4"))
                    End If
                ElseIf (CDbl(dr.Item("Vehicle_DWT")) < 200000) Then
                    If (dr.Item("Vehicle_Destination") & "" = "I") Then
                        dtTug.Rows(6).Item("Tug_Domestic") = CInt(dtTug.Rows(6).Item("Tug_Domestic")) + 1
                        dtTug.Rows(6).Item("Use_Domestic") = CInt(dtTug.Rows(6).Item("Use_Domestic")) + CInt(dr.Item("data3"))
                        dtTug.Rows(6).Item("Price_Domestic") = CDbl(dtTug.Rows(6).Item("Price_Domestic")) + CDbl(dr.Item("data4"))
                    ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                        dtTug.Rows(6).Item("Tug_Foreign") = CInt(dtTug.Rows(6).Item("Tug_Foreign")) + 1
                        dtTug.Rows(6).Item("Use_Foreign") = CInt(dtTug.Rows(6).Item("Use_Foreign")) + CInt(dr.Item("data3"))
                        dtTug.Rows(6).Item("Price_Foreign") = CDbl(dtTug.Rows(6).Item("Price_Foreign")) + CDbl(dr.Item("data4"))
                    End If
                ElseIf (CDbl(dr.Item("Vehicle_DWT")) < 250000) Then
                    If (dr.Item("Vehicle_Destination") & "" = "I") Then
                        dtTug.Rows(7).Item("Tug_Domestic") = CInt(dtTug.Rows(7).Item("Tug_Domestic")) + 1
                        dtTug.Rows(7).Item("Use_Domestic") = CInt(dtTug.Rows(7).Item("Use_Domestic")) + CInt(dr.Item("data3"))
                        dtTug.Rows(7).Item("Price_Domestic") = CDbl(dtTug.Rows(7).Item("Price_Domestic")) + CDbl(dr.Item("data4"))
                    ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                        dtTug.Rows(7).Item("Tug_Foreign") = CInt(dtTug.Rows(7).Item("Tug_Foreign")) + 1
                        dtTug.Rows(7).Item("Use_Foreign") = CInt(dtTug.Rows(7).Item("Use_Foreign")) + CInt(dr.Item("data3"))
                        dtTug.Rows(7).Item("Price_Foreign") = CDbl(dtTug.Rows(7).Item("Price_Foreign")) + CDbl(dr.Item("data4"))
                    End If
                ElseIf (CDbl(dr.Item("Vehicle_DWT")) < 350001) Then
                    If (dr.Item("Vehicle_Destination") & "" = "I") Then
                        dtTug.Rows(8).Item("Tug_Domestic") = CInt(dtTug.Rows(8).Item("Tug_Domestic")) + 1
                        dtTug.Rows(8).Item("Use_Domestic") = CInt(dtTug.Rows(8).Item("Use_Domestic")) + CInt(dr.Item("data3"))
                        dtTug.Rows(8).Item("Price_Domestic") = CDbl(dtTug.Rows(8).Item("Price_Domestic")) + CDbl(dr.Item("data4"))
                    ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                        dtTug.Rows(8).Item("Tug_Foreign") = CInt(dtTug.Rows(8).Item("Tug_Foreign")) + 1
                        dtTug.Rows(8).Item("Use_Foreign") = CInt(dtTug.Rows(8).Item("Use_Foreign")) + CInt(dr.Item("data3"))
                        dtTug.Rows(8).Item("Price_Foreign") = CDbl(dtTug.Rows(8).Item("Price_Foreign")) + CDbl(dr.Item("data4"))
                    End If
                End If
                dtTug.AcceptChanges()
            ElseIf (dr.Item("Type_Event") & "" = "F") Then
                'ฟรี
                Dim dtTugA As DataTable = clsGet.Get_Booking_LogSheet_Tug(dr.Item("Booking_no") & "", "", "W")
                Dim Tug As Integer = 0
                Dim Boat As Integer = 0
                Dim chkTug As Boolean = False
                For Each drTug As DataRow In dtTugA.Rows
                    If (drTug.Item("Tug_Name") & "" <> "") Then
                        Dim dtTugAType As DataTable = clsGet.Get_TugBoat(drTug.Item("Tug_Name"))
                        If (dtTugAType.Rows.Count > 0) Then
                            chkTug = True
                            If (dtTugAType.Rows(0).Item("Tug_Type") & "" = "0") Then
                                Tug += 1
                            Else
                                If (drTug.Item("Tug_Event") & "" = "W") Then
                                    Boat += 1
                                Else
                                    Tug += 1
                                End If
                            End If
                        End If


                    End If
                Next
                Dim Price As String = "0"
                If (chkTug) Then
                    If (Tug > 0) Then
                        Price = ComputeMoney("000000002100000236", "1", "1", "1", "1", "0", "", "", "0", Tug)
                    ElseIf (Boat > 0) Then
                        Price = ComputeMoney("000000002100000237", "1", "1", "1", "1", "0", "", "", "0", Boat)
                    End If
                End If
                If (Tug > 0) Or (Boat > 0) Then
                    If (CDbl(dr.Item("Vehicle_DWT")) < 2501) Then
                        If (Tug > 0) Then
                            If (dr.Item("Vehicle_Destination") & "" = "I") Then
                                dtTug.Rows(10).Item("Tug_Domestic") = CInt(dtTug.Rows(10).Item("Tug_Domestic")) + 1
                                dtTug.Rows(10).Item("Use_Domestic") = CInt(dtTug.Rows(10).Item("Use_Domestic")) + CInt(Tug)
                                dtTug.Rows(10).Item("Price_Domestic") = CDbl(dtTug.Rows(10).Item("Price_Domestic")) + CDbl(Price)
                            ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                                dtTug.Rows(10).Item("Tug_Foreign") = CInt(dtTug.Rows(10).Item("Tug_Foreign")) + 1
                                dtTug.Rows(10).Item("Use_Foreign") = CInt(dtTug.Rows(10).Item("Use_Foreign")) + CInt(Tug)
                                dtTug.Rows(10).Item("Price_Foreign") = CDbl(dtTug.Rows(10).Item("Price_Foreign")) + CDbl(Price)
                            End If
                        ElseIf (Boat > 0) Then
                            If (dr.Item("Vehicle_Destination") & "" = "I") Then
                                dtTug.Rows(9).Item("Tug_Domestic") = CInt(dtTug.Rows(9).Item("Tug_Domestic")) + 1
                                dtTug.Rows(9).Item("Use_Domestic") = CInt(dtTug.Rows(9).Item("Use_Domestic")) + CInt(Boat)
                                dtTug.Rows(9).Item("Price_Domestic") = CDbl(dtTug.Rows(9).Item("Price_Domestic")) + CDbl(Price)
                            ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                                dtTug.Rows(9).Item("Tug_Foreign") = CInt(dtTug.Rows(9).Item("Tug_Foreign")) + 1
                                dtTug.Rows(9).Item("Use_Foreign") = CInt(dtTug.Rows(9).Item("Use_Foreign")) + CInt(Boat)
                                dtTug.Rows(9).Item("Price_Foreign") = CDbl(dtTug.Rows(9).Item("Price_Foreign")) + CDbl(Price)
                            End If
                        End If

                    ElseIf (CDbl(dr.Item("Vehicle_DWT")) < 5001) Then
                        If (dr.Item("Vehicle_Destination") & "" = "I") Then
                            dtTug.Rows(11).Item("Tug_Domestic") = CInt(dtTug.Rows(11).Item("Tug_Domestic")) + 1
                            dtTug.Rows(11).Item("Use_Domestic") = CInt(dtTug.Rows(11).Item("Use_Domestic")) + CInt(Tug)
                            dtTug.Rows(11).Item("Price_Domestic") = CDbl(dtTug.Rows(11).Item("Price_Domestic")) + CDbl(Price)
                        ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                            dtTug.Rows(11).Item("Tug_Foreign") = CInt(dtTug.Rows(11).Item("Tug_Foreign")) + 1
                            dtTug.Rows(11).Item("Use_Foreign") = CInt(dtTug.Rows(11).Item("Use_Foreign")) + CInt(Tug)
                            dtTug.Rows(11).Item("Price_Foreign") = CDbl(dtTug.Rows(11).Item("Price_Foreign")) + CDbl(Price)
                        End If
                    ElseIf (CDbl(dr.Item("Vehicle_DWT")) < 10001) Then
                        If (dr.Item("Vehicle_Destination") & "" = "I") Then
                            dtTug.Rows(12).Item("Tug_Domestic") = CInt(dtTug.Rows(12).Item("Tug_Domestic")) + 1
                            dtTug.Rows(12).Item("Use_Domestic") = CInt(dtTug.Rows(12).Item("Use_Domestic")) + CInt(Tug)
                            dtTug.Rows(12).Item("Price_Domestic") = CDbl(dtTug.Rows(12).Item("Price_Domestic")) + CDbl(Price)
                        ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                            dtTug.Rows(12).Item("Tug_Foreign") = CInt(dtTug.Rows(12).Item("Tug_Foreign")) + 1
                            dtTug.Rows(12).Item("Use_Foreign") = CInt(dtTug.Rows(12).Item("Use_Foreign")) + CInt(Tug)
                            dtTug.Rows(12).Item("Price_Foreign") = CDbl(dtTug.Rows(12).Item("Price_Foreign")) + CDbl(Price)
                        End If
                    ElseIf (CDbl(dr.Item("Vehicle_DWT")) < 50000) Then
                        If (dr.Item("Vehicle_Destination") & "" = "I") Then
                            dtTug.Rows(13).Item("Tug_Domestic") = CInt(dtTug.Rows(13).Item("Tug_Domestic")) + 1
                            dtTug.Rows(13).Item("Use_Domestic") = CInt(dtTug.Rows(13).Item("Use_Domestic")) + CInt(Tug)
                            dtTug.Rows(13).Item("Price_Domestic") = CDbl(dtTug.Rows(13).Item("Price_Domestic")) + CDbl(Price)
                        ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                            dtTug.Rows(13).Item("Tug_Foreign") = CInt(dtTug.Rows(13).Item("Tug_Foreign")) + 1
                            dtTug.Rows(13).Item("Use_Foreign") = CDbl(dtTug.Rows(13).Item("Use_Foreign")) + CInt(Tug)
                            dtTug.Rows(13).Item("Price_Foreign") = CDbl(dtTug.Rows(13).Item("Price_Foreign")) + CDbl(Price)
                        End If
                    ElseIf (CDbl(dr.Item("Vehicle_DWT")) < 120000) Then
                        If (dr.Item("Vehicle_Destination") & "" = "I") Then
                            dtTug.Rows(14).Item("Tug_Domestic") = CInt(dtTug.Rows(14).Item("Tug_Domestic")) + 1
                            dtTug.Rows(14).Item("Use_Domestic") = CInt(dtTug.Rows(14).Item("Use_Domestic")) + CInt(Tug)
                            dtTug.Rows(14).Item("Price_Domestic") = CDbl(dtTug.Rows(14).Item("Price_Domestic")) + CDbl(Price)
                        ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                            dtTug.Rows(14).Item("Tug_Foreign") = CInt(dtTug.Rows(14).Item("Tug_Foreign")) + 1
                            dtTug.Rows(14).Item("Use_Foreign") = CDbl(dtTug.Rows(14).Item("Use_Foreign")) + CInt(Tug)
                            dtTug.Rows(14).Item("Price_Foreign") = CDbl(dtTug.Rows(14).Item("Price_Foreign")) + CDbl(Price)
                        End If
                    ElseIf (CDbl(dr.Item("Vehicle_DWT")) < 200000) Then
                        If (dr.Item("Vehicle_Destination") & "" = "I") Then
                            dtTug.Rows(15).Item("Tug_Domestic") = CInt(dtTug.Rows(15).Item("Tug_Domestic")) + 1
                            dtTug.Rows(15).Item("Use_Domestic") = CInt(dtTug.Rows(15).Item("Use_Domestic")) + CInt(Tug)
                            dtTug.Rows(15).Item("Price_Domestic") = CDbl(dtTug.Rows(15).Item("Price_Domestic")) + CDbl(Price)
                        ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                            dtTug.Rows(15).Item("Tug_Foreign") = CInt(dtTug.Rows(15).Item("Tug_Foreign")) + 1
                            dtTug.Rows(15).Item("Use_Foreign") = CDbl(dtTug.Rows(15).Item("Use_Foreign")) + CInt(Tug)
                            dtTug.Rows(15).Item("Price_Foreign") = CDbl(dtTug.Rows(15).Item("Price_Foreign")) + CDbl(Price)
                        End If
                    ElseIf (CDbl(dr.Item("Vehicle_DWT")) < 250000) Then
                        If (dr.Item("Vehicle_Destination") & "" = "I") Then
                            dtTug.Rows(16).Item("Tug_Domestic") = CInt(dtTug.Rows(16).Item("Tug_Domestic")) + 1
                            dtTug.Rows(16).Item("Use_Domestic") = CInt(dtTug.Rows(16).Item("Use_Domestic")) + CInt(Tug)
                            dtTug.Rows(16).Item("Price_Domestic") = CDbl(dtTug.Rows(16).Item("Price_Domestic")) + CDbl(Price)
                        ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                            dtTug.Rows(16).Item("Tug_Foreign") = CInt(dtTug.Rows(16).Item("Tug_Foreign")) + 1
                            dtTug.Rows(16).Item("Use_Foreign") = CDbl(dtTug.Rows(16).Item("Use_Foreign")) + CInt(Tug)
                            dtTug.Rows(16).Item("Price_Foreign") = CDbl(dtTug.Rows(16).Item("Price_Foreign")) + CDbl(Price)
                        End If
                    ElseIf (CDbl(dr.Item("Vehicle_DWT")) < 350001) Then
                        If (dr.Item("Vehicle_Destination") & "" = "I") Then
                            dtTug.Rows(17).Item("Tug_Domestic") = CInt(dtTug.Rows(17).Item("Tug_Domestic")) + 1
                            dtTug.Rows(17).Item("Use_Domestic") = CInt(dtTug.Rows(17).Item("Use_Domestic")) + CInt(Tug)
                            dtTug.Rows(17).Item("Price_Domestic") = CDbl(dtTug.Rows(17).Item("Price_Domestic")) + CDbl(Price)
                        ElseIf (dr.Item("Vehicle_Destination") & "" = "O") Then
                            dtTug.Rows(17).Item("Tug_Foreign") = CInt(dtTug.Rows(17).Item("Tug_Foreign")) + 1
                            dtTug.Rows(17).Item("Use_Foreign") = CDbl(dtTug.Rows(17).Item("Use_Foreign")) + CInt(Tug)
                            dtTug.Rows(17).Item("Price_Foreign") = CDbl(dtTug.Rows(17).Item("Price_Foreign")) + CDbl(Price)
                        End If
                    End If
                    dtTug.AcceptChanges()

                End If

            End If
        Next
        Return dtTug
    End Function

    Public Function CreateDataset() As DataSet
        Dim ds As New DataSet
        Dim dt As New DataTable
        ds.Tables.Add(dt)
        dt.Columns.Add("id", GetType(String))
        dt.Columns.Add("MAT_NAME", GetType(String))
        dt.Columns.Add("name", GetType(String))
        dt.Columns.Add("data1", GetType(String))
        dt.Columns.Add("data2", GetType(String))
        dt.Columns.Add("data3", GetType(String))
        dt.Columns.Add("data4", GetType(String))
        ds.Tables(0).AcceptChanges()
        ds.AcceptChanges()
        Return ds
    End Function

    Private Function ComputeMoney(ByVal MATERIAL As String, ByVal GRT As String, ByVal LOA As String, ByVal DWT As String, ByVal Draft As String, ByVal ProductValue As String, ByVal Customer_no As String, ByVal PORT As String, ByVal _StandardTime As String, ByVal Valuexx As String)
        Dim dtRelation As DataTable = clsGet.Get_MeterailRelation(MATERIAL)
        Dim ds As New DataSet
        ds = CreateDataset()
        If (dtRelation.Rows.Count > 0) Then
            Dim Value As Double = 0.0
            Select Case dtRelation.Rows(0).Item("Relation")
                Case "01" 'สูตรคำนวนน้ำหนักเรือ GRT 
                    Value = GRT
                Case "02" 'L.O.A.
                    Value = LOA
                Case "03" 'DWT
                    Value = DWT
                Case "04" 'Draft
                    Value = Draft
                Case "05" 'Product
                    Value = ProductValue
                Case "06"
                    Value = 0.0
            End Select
            Dim conditioncheck As Boolean = True
            For Each drR As DataRow In dtRelation.Rows

                If (conditioncheck) Then
                    'ทำการเปรียบเทียบช่วงน่ำหนักเรือ
                    If (drR.Item("Compute_Between") & "" = "A") Then
                        'แบบแรกตกช่วงคิด
                        If ((CDbl(drR("Point_Start")) <= Value) And (CDbl(drR("Point_End")) >= Value)) Then
                            'Rate ตกในช่วง
                            Dim rate As String = drR.Item("Mat_Rate")
                            If (dtRelation.Rows(0).Item("Customer_no") & "" <> "") Then
                                Dim Cus() As String = dtRelation.Rows(0).Item("Customer_no").ToString().Split(",")
                                For i As Integer = 0 To Cus.Length - 1
                                    If (Cus(i).ToString = Customer_no) Then
                                        rate = dtRelation.Rows(0).Item("Customer_Rate").ToString().Split(",")(i)
                                        Exit For
                                    End If
                                Next
                            End If
                            Dim drNew As DataRow = ds.Tables(0).NewRow()
                            drNew.Item("id") = CInt(drR.Item("Meterial")).ToString()
                            drNew.Item("name") = drR.Item("Mat_Name")
                            drNew.Item("MAT_NAME") = ""  'ไม่ได้แสดง
                            If (CInt(Value).ToString() = "0") Then
                                If (dtRelation.Rows(0).Item("Relation") & "" <> "05") Then
                                    Value = 1.0
                                End If
                                drNew.Item("data1") = ""
                            Else
                                drNew.Item("data1") = Value
                            End If
                            Try
                                drNew.Item("data2") = rate
                                Dim newValue As Double = 0
                                If (drR.Item("Point_Unit") = "0") Then
                                    newValue = 1
                                Else
                                    newValue = (Value / CDbl(drR.Item("Point_Unit")))
                                End If
                                Dim StardardTime As Double = 0
                                If (drR("data3") = "S") Or (drR("data3") = "C") Then
                                    StardardTime = CDbl(_StandardTime)
                                End If
                                If (drR("Data3") = "N") Then
                                    drNew.Item("data3") = ""
                                    drNew.Item("data4") = (newValue * CDbl(rate)).ToString("###############0.00")
                                ElseIf (drR("Data3") = "S") Then
                                    Session("Lay_Time") = StardardTime
                                    drNew.Item("data3") = StardardTime
                                    drNew.Item("data4") = ((newValue * CDbl(rate)) * StardardTime).ToString("###############0.00")
                                Else
                                    If (drR.Item("Compute_Count") & "" = "") Then
                                        If (drR.Item("Compute_C") & "" = "D") Then
                                            If (StardardTime = 0) Then
                                                drNew.Item("data3") = 1
                                            Else
                                                drNew.Item("data3") = ((CDbl(StardardTime) \ 24) + 1).ToString()
                                            End If

                                        Else
                                            If (Value Mod CDbl(drR.Item("Point_Unit"))) <> 0 Then
                                                drNew.Item("data3") = (CInt(Value \ CDbl(drR.Item("Point_Unit"))) + 1).ToString()
                                            Else
                                                drNew.Item("data3") = CInt(Value \ CDbl(drR.Item("Point_Unit"))).ToString()
                                            End If
                                        End If
                                    Else
                                        If (Valuexx = "") Then
                                            drNew.Item("data3") = drR.Item("Compute_Count")
                                        Else
                                            drNew.Item("data3") = Valuexx
                                        End If

                                    End If

                                    drNew.Item("data4") = (CDbl(rate) * CDbl(drNew.Item("data3"))).ToString("###############0.00")
                                End If
                                If (drR.Item("LowRate") & "" = "") Then
                                    drR.Item("LowRate") = "0"
                                End If
                                If (CDbl(drR.Item("LowRate")) > 0.0) Then
                                    If (CDbl(drR.Item("LowRate")) > CDbl(drNew.Item("data4"))) Then
                                        'ราคาต่ำกว่า ราคาขั้นต่ำ
                                        If (drNew.Item("data3") = "") Then
                                            drNew.Item("data2") = drR.Item("LowRate")
                                            drNew.Item("data4") = drR.Item("LowRate")
                                        Else
                                            drNew.Item("data4") = drR.Item("LowRate")
                                            drNew.Item("data2") = CDbl(drR.Item("LowRate") / drNew.Item("data3")).ToString("################0.00")
                                        End If
                                    End If
                                End If
                            Catch ex As Exception
                                '((LOA-5)*Draft)+10%
                                If (drR.Item("LowRate") & "" = "") Then
                                    drR.Item("LowRate") = "0"
                                End If
                                Dim LoACompute As Double = ((LOA - 5.0) * Draft) + ((((LOA - 5.0) * Draft) * 10) / 100).ToString("##################0.00")
                                If (CDbl(drR.Item("LowRate")) > LoACompute) Then
                                    drNew.Item("data2") = drR.Item("LowRate")
                                    drNew.Item("data3") = "1"
                                    drNew.Item("data4") = drR.Item("LowRate")
                                Else
                                    drNew.Item("data2") = LoACompute.ToString()
                                    drNew.Item("data3") = "1"
                                    drNew.Item("data4") = LoACompute.ToString()
                                End If
                            End Try
                            ds.Tables(0).Rows.Add(drNew)
                            ds.Tables(0).AcceptChanges()
                            Exit For
                        End If
                    Else
                        'แบบสองผ่านช่วงไหนคิดหมด
                        If ((CDbl(drR("Point_Start")) <= Value)) Then
                            'Rate ตกในช่วง
                            Dim drNew As DataRow = ds.Tables(0).NewRow()
                            drNew.Item("id") = CInt(drR.Item("Meterial")).ToString()
                            drNew.Item("name") = drR.Item("Mat_Name")
                            drNew.Item("MAT_NAME") = "" 'ไม่ม่ชื่อ
                            Dim Nvalue As Double = 0
                            If (Value > drR("Point_End")) Then
                                Nvalue = drR("Point_End") - CDbl(drR("Point_Start"))
                            Else
                                Nvalue = Value - CDbl(drR("Point_Start"))
                            End If
                            Dim rate As String = drR.Item("Mat_Rate")
                            If (dtRelation.Rows(0).Item("Customer_no") & "" <> "") Then
                                Dim Cus() As String = dtRelation.Rows(0).Item("Customer_no").ToString().Split(",")
                                For i As Integer = 0 To Cus.Length - 1
                                    If (Cus(i).ToString = Customer_no) Then
                                        rate = dtRelation.Rows(0).Item("Customer_Rate").ToString().Split(",")(i)
                                        Exit For
                                    End If
                                Next
                            End If
                            drNew.Item("data1") = Nvalue
                            drNew.Item("data2") = rate

                            Dim newValue As Double = 0
                            If (drR.Item("Point_Unit") = "0") Then
                                newValue = 1
                            Else
                                newValue = (Nvalue / CDbl(drR.Item("Point_Unit")))
                            End If
                            Dim StardardTime As Double = 0
                            If (drR("data3") = "S") Or (drR("data3") = "C") Then
                                StardardTime = _StandardTime
                            End If
                            If (drR("Data3") = "N") Then
                                drNew.Item("data3") = ""
                                drNew.Item("data4") = (newValue * CDbl(rate)).ToString("###############0.00")

                            ElseIf (drR("Data3") = "S") Then
                                'ทำการคำนวณหา Stardard Time
                                'If (StardardTime = 0) Then
                                '    StardardTime = 1
                                'End If
                                Session("Lay_Time") = StardardTime
                                drNew.Item("data3") = StardardTime
                                drNew.Item("data4") = ((newValue * CDbl(rate)) * StardardTime).ToString("###############0.00")
                            Else
                                If (drR.Item("Compute_Count") & "" = "") Then
                                    If (drR.Item("Compute_C") & "" = "D") Then
                                        drNew.Item("data3") = (CDbl(StardardTime) \ 24).ToString()
                                    Else
                                        If (Value Mod CDbl(drR.Item("Point_Unit"))) <> 0 Then
                                            drNew.Item("data3") = (CInt(Value \ CDbl(drR.Item("Point_Unit"))) + 1).ToString()
                                        Else
                                            drNew.Item("data3") = CInt(Value \ CDbl(drR.Item("Point_Unit"))).ToString()
                                        End If
                                    End If
                                Else
                                    drNew.Item("data3") = drR.Item("Compute_Count")
                                End If

                                drNew.Item("data4") = (CDbl(rate) * CDbl(drNew.Item("data3"))).ToString("###############0.00")
                            End If
                            If (drR.Item("LowRate") & "" = "") Then
                                drR.Item("LowRate") = "0"
                            End If
                            If (CDbl(drR.Item("LowRate")) > 0.0) Then
                                If (CDbl(drR.Item("LowRate")) > CDbl(drNew.Item("data4"))) Then
                                    'ราคาต่ำกว่า ราคาขั้นต่ำ
                                    If (drNew.Item("data3") = "") Then
                                        drNew.Item("data2") = drR.Item("LowRate")
                                        drNew.Item("data4") = drR.Item("LowRate")
                                    Else
                                        drNew.Item("data4") = drR.Item("LowRate")
                                        drNew.Item("data2") = CDbl(drR.Item("LowRate") / drNew.Item("data3")).ToString("################0.00")
                                    End If
                                End If
                            End If

                            ds.Tables(0).Rows.Add(drNew)
                            ds.Tables(0).AcceptChanges()
                            Exit For
                        End If
                    End If
                End If
            Next
        Else
            'ไม่พบข้อมูลการผูกสูตร
            Dim Plant As String = "1104"
            If (PORT = "L") Then
                Plant = "1103"
            End If
            dtRelation = clsGet.Get_SubMeterail(MATERIAL)
            If (dtRelation.Rows.Count > 0) Then
                For Each dm As DataRow In dtRelation.Rows
                    If (Plant = dm.Item("WERKS")) Then
                        Dim drNew As DataRow = ds.Tables(0).NewRow()
                        Dim id As String = ""
                        Try
                            id = CInt(MATERIAL).ToString()
                        Catch ex As Exception
                            id = MATERIAL
                        End Try
                        drNew.Item("id") = id
                        drNew.Item("name") = ""
                        drNew.Item("MAT_NAME") = ""
                        drNew.Item("data1") = ""
                        drNew.Item("data2") = dm.Item("KBETR")
                        drNew.Item("data3") = "1"


                        drNew.Item("data4") = CDbl(dm.Item("KBETR") * CDbl("1")).ToString("###############0.00")
                        ds.Tables(0).Rows.Add(drNew)
                        ds.Tables(0).AcceptChanges()
                    End If
                Next
            End If
        End If
        Return ds.Tables(0).Rows(0).Item("data4")
    End Function
End Class